import csv
import sys
#import time
#import pdb
from PyQt4.QtCore import (QFile,QIODevice,QDate,QTime,Qt)


##Pants: Need to add alot of error checking here
#################################################

sfilename = 'temp.csv'

def pData(csvfile): #QtCore.QTime.fromString(pData()[77][4])
    '''Use Python CSV module to open and split CSV file'''
    #starttime = time.time()
    sfile = open(csvfile)
    kanji = csv.reader(sfile, delimiter=',')
    kanji.next()
    complist = []
    goodstanding = True
    while goodstanding:
        try:
            x = kanji.next()
            
            ## Extract date and time
            ## And assign to QDate and
            ## QTime variables
            time = QTime.fromString(x[4].split(' ')[1])
            date = QDate.fromString(x[4].split(' ')[0],1)
            ##Pants: Could add in a time/date.IsValid() someday
            complist.append([int(x[0]),
                             unicode(x[1],'utf_8'),
                             unicode(x[2],'utf_8'),
                             bool(int(x[3])),
                             [date,time],
                             unicode(x[5],'utf_8'),
                             ])

        except StopIteration:
            goodstanding = False
    sfile.close()
    #endtime = time.time()
    #print endtime-starttime
    return complist


## This function is broken, but it is
## about 2/3rds faster than pData()
########################################

def qData(csvfile):
    '''[BROKEN] Use Qt API to open and split CSV file'''
    starttime = time.time()
    complist = []
    qfile = QFile(csvfile)
    if not qfile.fileName():
        return False
    else:
        #pdb.set_trace()
        if qfile.open(QIODevice.ReadOnly):
            qfile.readLine()
            while not qfile.atEnd():
                
                x = qfile.readLine().split(',')
                complist.append(x)
            qfile.close()
            endtime = time.time()
            print endtime-starttime
            return complist
                
        else:
            return Flase






